from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    ROLE_CHOICES = (
        ('customer', '顾客'),
        ('technician', '技师'),
        ('admin', '管理员'),
    )
    
    openid = models.CharField(max_length=128, unique=True, verbose_name='微信OpenID')
    role = models.CharField(max_length=20, choices=ROLE_CHOICES, default='customer', verbose_name='用户角色')
    phone = models.CharField(max_length=20, blank=True, verbose_name='手机号')
    avatar = models.URLField(blank=True, verbose_name='头像')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'user_info'
        verbose_name = '用户信息'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return self.username


class Technician(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='technician_profile')
    real_name = models.CharField(max_length=50, verbose_name='真实姓名')
    id_card = models.CharField(max_length=18, unique=True, verbose_name='身份证号')
    id_card_front = models.URLField(verbose_name='身份证正面')
    id_card_back = models.URLField(verbose_name='身份证反面')
    certification = models.URLField(blank=True, verbose_name='资格证书')
    service_area = models.CharField(max_length=100, verbose_name='服务区域')
    experience = models.TextField(blank=True, verbose_name='经验介绍')
    is_verified = models.BooleanField(default=False, verbose_name='是否认证')
    rating = models.DecimalField(max_digits=3, decimal_places=2, default=5.00, verbose_name='评分')
    total_orders = models.IntegerField(default=0, verbose_name='总订单数')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'technician_info'
        verbose_name = '技师信息'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return f"{self.real_name} ({self.user.username})"